﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.Core.Extensions;
using System.Linq;
using System.Collections.Generic;
using TyMES.Model.Record;
using TyMES.ViewModel.Record.BadProductRecordVMs;
using TyMES.Model;
using TyMES.Services;
using Microsoft.Extensions.Logging;
using TyMES.PassStationCommunication;
using Microsoft.AspNetCore.Authorization;

namespace TyMES.Record.Controllers
{
    [AuthorizeJwtWithCookie]
    [ActionDescription("_Model.BadProductRecord")]
    [ApiController]
    [Route("/api/Record/BadProductRecord")]
    public partial class BadProductRecordController : BaseApiController
    {
        private readonly BadProductService _badProductService;

        public BadProductRecordController(BadProductService badProductService)
        {
            _badProductService = badProductService;
        }

        [ActionDescription("Sys.Get")]
        [HttpGet("{id}")]
        public IActionResult Get(string id)
        {
            var vm = Wtm.CreateVM<BadProductRecordVM>(id);
            return Ok(vm);
        }

        [ActionDescription("Sys.Create")]
        [HttpPost("[action]")]
        public async Task<IActionResult> Create(BadProductRecordVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoAddAsync();
   
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }

        }

        [ActionDescription("Sys.Edit")]
        [HttpPut("[action]")]
        public async Task<IActionResult> Edit(BadProductRecordVM vm)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                await vm.DoEditAsync(false);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState.GetErrorJson());
                }
                else
                {
                    return Ok(vm.Entity);
                }
            }
        }
                

        [HttpPost("BatchEdit")]
        [ActionDescription("Sys.BatchEdit")]
        public ActionResult BatchEdit(BadProductRecordBatchVM vm)
        {
            if (!ModelState.IsValid || !vm.DoBatchEdit())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(vm.Ids.Count());
            }
        }

		[HttpPost("BatchDelete")]
        [ActionDescription("Sys.Delete")]
        public IActionResult BatchDelete(string[] ids)
        {
            var vm = Wtm.CreateVM<BadProductRecordBatchVM>();
            if (ids != null && ids.Count() > 0)
            {
                vm.Ids = ids;
            }
            else
            {
                return Ok();
            }
            if (!ModelState.IsValid || !vm.DoBatchDelete())
            {
                return BadRequest(ModelState.GetErrorJson());
            }
            else
            {
                return Ok(ids.Count());
            }
        }

        [ActionDescription("Sys.DownloadTemplate")]
        [HttpGet("GetExcelTemplate")]
        public IActionResult GetExcelTemplate()
        {
            var vm = Wtm.CreateVM<BadProductRecordImportVM>();
            var qs = new Dictionary<string, string>();
            foreach (var item in Request.Query.Keys)
            {
                qs.Add(item, Request.Query[item]);
            }
            vm.SetParms(qs);
            var data = vm.GenerateTemplate(out string fileName);
            return File(data, "application/vnd.ms-excel", fileName);
        }

        [ActionDescription("Sys.Import")]
        [HttpPost("Import")]
        public ActionResult Import(BadProductRecordImportVM vm)
        {

            if (vm.ErrorListVM.EntityList.Count > 0 || !vm.BatchSaveData())
            {
                return BadRequest(vm.GetErrorJson());
            }
            else
            {
                return Ok(vm.EntityList.Count);
            }
        }

        [AllowAnonymous]
        [ActionDescription("检查输入的工号和密码是否正确")]
        [HttpPost("Check")]
        public async Task<ActionResult> Check(VerifierRecord v)
        {
            try
            {
                var res = await _badProductService.CheckIdAndPassword(v);
                return Ok(res);
            }
            catch (Exception ex)
            {
                return Ok(new {Success = false, Mesg = "服务异常"+ex.Message });
            }          
        }

        [AllowAnonymous]
        [ActionDescription("新增确认人")]
        [HttpPost("AddPeople")]
        public async Task<ActionResult> AddPeople(VerifierRecord v)
        {
            try
            {
                var res = await _badProductService.Addpeople(v);
                return Ok(res);
            }
            catch (Exception ex)
            {
                return Ok(new { Success = false, Mesg = "服务异常" + ex.Message });
            }
        }

        [AllowAnonymous]
        [ActionDescription("人工确认不良")]
        [HttpPost("ConfirmNG")]
        public async Task<ActionResult> ConfirmNG(BadProductRecord bp)
        {
            try
            {
                var res = await _badProductService.ConfirmBadproNG(bp);
                return Ok(res);
            }
            catch (Exception ex)
            {
                return Ok(new { Success = false, Mesg = "服务异常" + ex.Message });
            }
        }

        [AllowAnonymous]
        [ActionDescription("人工确认合格")]
        [HttpPost("ConfirmOK")]
        public async Task<ActionResult> ConfirmOK(BadProductRecord bp)
        {
            try
            {
                var res = await _badProductService.ConfirmBadproOK(bp);
                return Ok(res);
            }
            catch (Exception ex)
            {
                return Ok(new { Success = false, Mesg = "服务异常" + ex.Message });
            }
        }
    }
}